import request from "@/http/request";

export function selectOrderVOByCustomerId(customerId,pageNum,pageSize){
    let url="/api/order/customerId";
    return request.get(url,{
        params:{
            pageNum,pageSize,customerId
        }
    })
}

export function updateOrderToPay(orderId){
    let url="/api/order/pay";
    return request.post(url,null,{
        params:{
            orderId
        }
    })
}

export function selectOrderByOrderId(orderId){
    let url="/api/order/byOrderId";
    return request.get(url,{
        params:{
            orderId
        }
    })
}

export function updateOrderStatusUnsubscribe(orderId){
    let url = "/api/order/unsubscribe";
    return request.post(url,null,{
        params:{
            orderId
        }
    })
}

export function insertOrder(data){
    let xmlStr = getXmlBody(data)
    return new Promise(((resolve, reject) => {
        request.post("/api/webService/server",xmlStr,{
            headers:{
                'Content-Type':'text/xml'
            }
        }).then(res=>{
            try {
                let xmlData = xmlObj2json(res.data)
                res.data = {
                    ...xmlData['soap:Envelope']['soap:Body']['ns2:returnResult']
                }
                resolve(res)
            }catch (e){
                console.log(e)
                reject(res)
            }
        })
    }))
}

function getXmlBody(data){
    let reqStr = JSON.stringify(data)
    console.log(data)
    reqStr = encodeURIComponent(reqStr)
    let xmlStr = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.qydcdev.hellowx.com/">\n' +
        '   <soapenv:Header/>\n' +
        '   <soapenv:Body>\n' +
        '      <web:createOrder>'+reqStr+'</web:createOrder>\n' +
        '   </soapenv:Body>\n' +
        '</soapenv:Envelope>';
    return xmlStr;
}

/**
 * xml字符串转换xml对象数据
 * @param {Object} xmlStr
 */
function xmlStr2XmlObj(xmlStr) {
    let xmlObj = {};
    if (document.all) {
        let xmlDom = new ActiveXObject("Microsoft.XMLDOM");
        xmlDom.loadXML(xmlStr);
        xmlObj = xmlDom;
    } else {
        xmlObj = new DOMParser().parseFromString(xmlStr, "text/xml");
    }
    return xmlObj;
}

/**
 * xml字符串转换json数据
 * @param {Object} xml
 */
function xmlObj2json(xml) {
    var xmlObj = xmlStr2XmlObj(xml);
    var jsonObj = {};
    if (xmlObj.childNodes.length > 0) {
        jsonObj = xml2json(xmlObj);
    }
    return jsonObj;
}

/**
 * xml转换json数据
 * @param {Object} xml
 */
function xml2json(xml) {
    try {
        var obj = {};
        if (xml.children.length > 0) {
            for (var i = 0; i < xml.children.length; i++) {
                var item = xml.children.item(i);
                var nodeName = item.nodeName;
                if (typeof(obj[nodeName]) == "undefined") {
                    obj[nodeName] = xml2json(item);
                } else {
                    if (typeof(obj[nodeName].push) == "undefined") {
                        var old = obj[nodeName];
                        obj[nodeName] = [];
                        obj[nodeName].push(old);
                    }
                    obj[nodeName].push(xml2json(item));
                }
            }
        } else {
            obj = xml.textContent;
        }
        return obj;
    } catch (e) {
        console.log(e.message);
    }
}
